package com.kingsoft.miot.bridge.config;

import javax.servlet.MultipartConfigElement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
public class SpringMvcConfig extends WebMvcConfigurerAdapter {

	// 自动注入spring boot默认的上传配置
	@Autowired
	private MultipartConfigElement multipartConfigElement;

//	@Autowired
//	private DispatcherServlet dispatcherServlet;

	/**
	 * 路径映射配置  可不配
	 * 
	 * @return
	 */
//	@Bean
//	public ServletRegistrationBean apiServlet() {
//		ServletRegistrationBean bean = new ServletRegistrationBean(dispatcherServlet);
//		// 注入上传配置到自己注册的ServletRegistrationBean
//		bean.addUrlMappings("/*");
//		bean.setMultipartConfig(multipartConfigElement);
//		bean.setName("apiServlet");
//		return bean;
//	}

	/**
	 * 添加过滤器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
//		registry.addInterceptor(new LoginInterceptor());
//		registry.addInterceptor(new AccessInterceptor());
	}

	// 设置session的有效时间  一个小时
	@Bean
	public EmbeddedServletContainerCustomizer containerCustomizer() {
		return new EmbeddedServletContainerCustomizer() {
			@Override
			public void customize(ConfigurableEmbeddedServletContainer container) {
				container.setSessionTimeout(3600);// 单位为S
			}
		};
	}

}
